package com.fdb.efp.console.api.rest;

import com.alibaba.fastjson.JSON;
import com.fdb.basic.framework.core.constant.ApplicationConstance;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.console.service.facade.SOrgService;
import com.fdb.efp.console.service.facade.SUserOrgService;
import com.fdb.efp.console.service.facade.SUserService;
import com.fdb.efp.console.service.vo.SOrgVO;
import com.fdb.efp.console.service.vo.SUserOrgVO;
import com.fdb.efp.console.service.vo.SUserVO;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/api")
public class SOrgController extends AbstractController  {
	
	@Autowired
	@Qualifier("sOrgService")
	private SOrgService sOrgService;

	@Autowired
	@Qualifier("sUserOrgService")
	private SUserOrgService sUserOrgService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	@Autowired
	@Qualifier("sUserService")
	private SUserService sUserService;

	/**
	 * 列表数据查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/s/orgs", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SOrgVO>> querySOrgAll(@RequestBody SOrgVO vo) {
		List<SOrgVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sOrgService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = sOrgService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sOrgService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sOrgService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @param orgCode
	 * @return
	 */
	@RequestMapping(value="/s/org/{orgCode}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SOrgVO> queryByOrgCode(@PathVariable("orgCode") String orgCode) {
		SOrgVO inVo = new SOrgVO();
		inVo.setOrgCode(orgCode);
		SOrgVO outputVo = sOrgService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 机构在页面不进行物理删除，只是更新状态
	 * @date 2019/11/12
	 * @param inSOrgVo
	 * @return
	 */
	@RequestMapping(value="/s/org", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByOrgCode(@RequestBody SOrgVO inSOrgVo) {
		//int outputVo = sOrgService.deleteByPk(inSOrgVo);
		setUserInfoToVO(inSOrgVo);
		List<SUserVO> tempVo = sUserService.queryUSByOrgCode(inSOrgVo.getOrgCode());
		if (Objects.nonNull(tempVo) && !tempVo.isEmpty()) {
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setMessage("该机构下存在用户，不允许注销此机构！");
			responseData.setCode(ApplicationConstance.FAILURE);
			responseData.setSuccess(false);
			return responseData;
		}
		int outputVo = sOrgService.updateByPk(inSOrgVo);
		return getResponseData(outputVo);

	}
	
	/**
	 * 根据主键更新信息
	 * @param inSOrgVo
	 * @return
	 */
	@RequestMapping(value="/s/org", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SOrgVO inSOrgVo) {
		setUserInfoToVO(inSOrgVo);
		inSOrgVo.setLastUpdateUser(inSOrgVo.getLoginUserId());
		inSOrgVo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
		int outputVo = sOrgService.updateByPk(inSOrgVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSOrgVo
	 * @return
	 */
	@RequestMapping(value="/s/org", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSOrg(@RequestBody SOrgVO inSOrgVo) {
		setUserInfoToVO(inSOrgVo);
		inSOrgVo.setCreateUser(inSOrgVo.getLoginUserId());
		inSOrgVo.setCreateTime(TimeUtil.getCurrentDateTime());
		/**
		 * 根据需求新增 
		 * @author qiuyf
		 * @time 2019-03-02 16:14
		 * 【变更内容】当页面新增机构信息，根据页面输入的机构层级、上级机构代码获取应该新增的location字段值
		 * 
		 */
		if(StringUtil.isStrNotEmpty(inSOrgVo.getOrgCode()) && StringUtil.isStrNotEmpty(inSOrgVo.getOrgLevel()) && StringUtil.isStrNotEmpty(inSOrgVo.getOrgParentCode())) {
			//查询 = 机构层级 -1 
			SOrgVO doVO = new SOrgVO();
			doVO.setOrgLevel(String.valueOf((Integer.parseInt(inSOrgVo.getOrgLevel())-1)));
			List<SOrgVO> sOrgVOList = sOrgService.queryAllOrgsNoPage(doVO);
			sOrgVOList.forEach(item -> {
				if(StringUtil.isStrNotEmpty(item.getLocation()) && item.getLocation().endsWith(inSOrgVo.getOrgParentCode())) {
					inSOrgVo.setLocation(item.getLocation() + "," + inSOrgVo.getOrgCode());
				}
			});
		}
		int outputVo = sOrgService.insertSOrg(inSOrgVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 获取当前登录下当前机构及下属机构
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/s/orgs/login/leage", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SOrgVO>> queryLoginUserLeageOrgCode(@RequestBody SOrgVO vo) {
		setUserInfoToVO(vo);
		/**
		 * 用于筛选上级机构
		 * @since 2019-03-04 15:56:49
		 * @author qiuyf
		 */
		if(StringUtil.isStrNotEmpty(vo.getOrgLevel())) {
			vo.setOrgLevel(String.valueOf(Integer.parseInt(vo.getOrgLevel())-1));
		}
		List<SOrgVO> outputVo = sOrgService.queryAllCurrDownOrg(vo);
		return getResponseData(outputVo);
	}
	
	/**
	 * 查询所有法人机构
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/s/org/legals", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SOrgVO>> queryLeageOrgs(@RequestBody SOrgVO vo) {
		setUserInfoToVO(vo);
		List<SOrgVO> outputVo = sOrgService.queryLeageOrgs(vo);
		return getResponseData(outputVo);
	}
	
	@RequestMapping(value="/s/org/trees", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SOrgVO>> queryOrgTrees(@RequestBody SOrgVO vo) {
		setUserInfoToVO(vo);
		List<SOrgVO> outputVo = sOrgService.queryOrgTrees(vo);
		return getResponseData(outputVo);
		
		
	} 
	/**
	 * 获取当前登录下的下属机构,根据法人机构过滤
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/s/orgs/login/only/leage", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SOrgVO>> queryLoginOnlyLegalOrgCode(@RequestBody SOrgVO vo) {
		setUserInfoToVO(vo);
		List<SOrgVO> outputVo = sOrgService.queryLoginOnlyLegalOrgCode(vo);
		return getResponseData(outputVo);
	}
	
	/**
	 * 根据主键更新机构简称
	 * @param inSOrgVo
	 * @return
	 */
	@RequestMapping(value="/s/org/simple/name", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> updateOrgSimpleName(@RequestBody SOrgVO inSOrgVo) {
		setUserInfoToVO(inSOrgVo);
		inSOrgVo.setLastUpdateUser(inSOrgVo.getLoginUserId());
		inSOrgVo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
		int outputVo = sOrgService.updateOrgSimpleName(inSOrgVo);
		
		return getResponseData(outputVo);
	}
	
	/**
	 * 根据查询条件 查询机构
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/s/orgs/condition", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SOrgVO>> queryOrgByCondition(@RequestBody SOrgVO vo) {
		setUserInfoToVO(vo);
		List<SOrgVO> outputVo = sOrgService.queryOrgByCondition(vo);
		return getResponseData(outputVo);
	}


	@RequestMapping(value="/check/s/org", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> checkOrgCode(@RequestBody SOrgVO vo) {
		SOrgVO sOrgVO = sOrgService.queryByPk(vo);
		if(Objects.nonNull(sOrgVO)){
			logger.error("已存在机构号为【" + sOrgVO.getOrgCode() + "】的机构信息:" + JSON.toJSONString(sOrgVO));
			return ResponseData.create(-1, ApplicationConstance.FAILURE, null, false);
		}
		return getResponseData(0);
	}

}
